<?php
/**
 * Created by PhpStorm.
 * User: allen
 * Date: 2019/1/23
 * Time: 17:37
 */
namespace app\admin\model;

use think\Model;

class File extends Model{
    /**
     * @param int $p
     * @param int $limit
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getList($p=1,$limit=10){
        $list = $this
            ->limit($limit)
            ->page($p)
            ->select();
        $list = $list->toArray();
        return $list;
    }
    /**
     * @param string $key
     * @return array|\PDOStatement|string|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getOneByKey($key=''){
        $info = $this
            ->with('type')
            ->where('name|picurl','eq',$key)
            ->find();
        return $info;
    }

    public function type(){
        return $this->hasOne('UploadType','id','tid')
            ->bind([
                'type_id'=>'id',
                'type_name'=>'name',
                'bucket',
                'finder'
            ]);
    }

    /**
     * @param array $where
     * @param string $order
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getMenuList($where=[],$order=''){
        $list = $this
            ->where($where)
            ->order($order)
            ->select();
        return $list;
    }

    /**
     * @param $name
     * @param $picUrl
     * @param $type
     * @param $w
     * @param $h
     * @param $size
     * @param $tid
     * @return int|string
     */
    public function addNew($name,$picUrl,$type,$w,$h,$size,$tid){
        $data['name'] = $name;
        $data['tid'] = $tid;
        $data['picurl'] = $picUrl;
        $data['type'] = $type;
        $data['w'] = $w;
        $data['h'] = $h;
        $data['size'] = $size;
        $data['create_time'] = time();
        $id = $this->insertGetId($data);
        if($id){
            return $id;
        }else{
            return 0;
        }
    }

    /**
     * @param $code
     * @param string $url
     * @param boolean $false
     * @return bool|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getUrlById($code,&$url='',$false=false){
        if(!$code){
            return '';
        }
        if(is_array($code)){
            $list = $this::field('id as name,picurl as url')->whereIn('id',$code)->select();
            $url = $list;
        }else{
            $list = $this::field('id,picurl')->find($code);
            if($false){
                $url[] = $list['picurl'];
            }
            $url = $list['picurl'];
        }
        return $url;
    }

    /**
     *
     * @param $code
     * @return array|bool|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getUrlsById($code){
        if(!$code){
            return false;
        }
        $url = [];
        if(is_array($code)){
            $list = $this::field('id as name,picurl as url')->whereIn('id',$code)->select();
            $url = $list;
        }else{
            $list = $this::field('id,picurl as url')->find($code);
            $url[] = $list;
        }
        return $url;
    }
}